<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">

    <h:head>
    </h:head>
    <h:body>

        <ui:composition template="/resources/template/template.xhtml">

            <ui:define name="content">

                <h:form>
                    <p:panel header="Manage Item Fees" >
                        <p:panelGrid columns="1" styleClass="alignTop" >
                            <p:panelGrid columns="2" styleClass="alignTop" >
                                <h:outputLabel value="Select Item" />
                                <p:autoComplete widgetVar="aIx" id="acIx" 
                                                forceSelection="true" 
                                                value="#{itemFeeManager.item}"
                                                completeMethod="#{itemController.completeInwardItems}" 
                                                var="ix" 
                                                itemLabel="#{ix.name}" itemValue="#{ix}" size="30"  
                                                style="width: 400px;">
                                    <p:ajax event="itemSelect" process="@this" update="gpFees lblTotal" listener="#{itemFeeManager.fillFees()}"></p:ajax>
                                    <p:column headerText="Type" >
                                        <p:outputLabel value="#{ix.class.simpleName}" ></p:outputLabel>
                                    </p:column>
                                    <p:column headerText="Name" >
                                        <p:outputLabel value="#{ix.name}" ></p:outputLabel>
                                    </p:column>
                                    <p:column headerText="Institution" >
                                        <p:outputLabel value="#{ix.institution.name}" ></p:outputLabel>
                                    </p:column>
                                    <p:column headerText="Department" >
                                        <p:outputLabel value="#{ix.department.name}" ></p:outputLabel>
                                    </p:column>
                                    <p:column headerText="Total" >
                                        <p:outputLabel value="#{ix.total}" >
                                            <f:convertNumber pattern="#,##0.00" ></f:convertNumber>
                                        </p:outputLabel>
                                    </p:column>
                                </p:autoComplete>

                                <h:outputLabel value="Total Fee" />
                                <h:outputLabel id="lblTotal" value="#{itemFeeManager.item.total}" >
                                    <f:convertNumber pattern="#,##0.00" ></f:convertNumber>
                                </h:outputLabel>

                                <h:outputLabel value="Total Fee for Foreigners" />
                                <h:outputLabel id="lblTotalF" value="#{itemFeeManager.item.totalForForeigner}" >
                                    <f:convertNumber pattern="#,##0.00" ></f:convertNumber>
                                </h:outputLabel>


                                <p:commandButton value="Add New Fee" process="acIx" 
                                                 onclick="PF('dlg').show();">   
                                </p:commandButton>
                                
                                <p:commandButton value="Back" ajax="false"
                                                 action="#{viewController.backToPreviousPage()}">   
                                </p:commandButton>

                            </p:panelGrid>

                            <p:dialog id="dlg"  header="Adding New Fee" widgetVar="dlg" modal="true" height="400" >
                                <f:facet name="header" >
                                    <p:commandButton value="Save" action="#{itemFeeManager.addNewFee()}" style="margin-left: 30px;" 
                                                     update="gpFees lblTotal dlg" process="gpDetail @this" oncomplete="PF('dlg').hide();">
                                    </p:commandButton>
                                </f:facet>
                                <p:panel  id="gpDetail" >
                                    <p:panelGrid columns="2" >
                                        <h:outputLabel value="Fee Name" ></h:outputLabel>
                                        <h:inputText value="#{itemFeeManager.itemFee.name}" >
                                            <f:ajax event="keyup" execute="@this" ></f:ajax>
                                        </h:inputText>

                                        <h:outputLabel value="Fee Type" ></h:outputLabel>
                                        <p:selectOneMenu value="#{itemFeeManager.itemFee.feeType}" >
                                            <f:selectItem itemLabel="Select Fee Type" ></f:selectItem>
                                            <f:selectItems value="#{enumController.feeTypes}" var="ft" itemLabel="#{ft.label}" itemValue="#{ft}" />                                        
                                            <p:ajax process="@this" update="gpDetail" ></p:ajax>
                                        </p:selectOneMenu>

                                        <h:outputLabel value="Discounts Allowed" ></h:outputLabel>
                                        <p:selectBooleanButton value="#{itemFeeManager.itemFee.discountAllowed}" offLabel="Discounts NOT Allowed" onLabel="Discounts Allowed" >
                                        </p:selectBooleanButton>

                                        <h:panelGroup id="gpLblIns" rendered="#{itemFeeManager.itemFee.feeType eq 'OtherInstitution' or itemFeeManager.itemFee.feeType eq 'OwnInstitution'  or itemFeeManager.itemFee.feeType eq 'Referral' }" >
                                            <h:outputLabel value="Institution" ></h:outputLabel>
                                        </h:panelGroup>

                                        <h:panelGroup id="gpAcIns" rendered="#{itemFeeManager.itemFee.feeType eq 'OtherInstitution' or itemFeeManager.itemFee.feeType eq 'OwnInstitution'  or itemFeeManager.itemFee.feeType eq 'Referral' }" >
                                            <p:autoComplete id="acIns" value="#{itemFeeManager.itemFee.institution}" 
                                                            completeMethod="#{institutionController.completeIns}"
                                                            var="ins" 
                                                            itemLabel="#{ins.name}" 
                                                            itemValue="#{ins}" 
                                                            converter="institutionConverter" >
                                                <p:ajax event="itemSelect" process="acIns" update="gpSomDepartment" listener="#{itemFeeManager.fillDepartments()}" ></p:ajax>
                                            </p:autoComplete>
                                        </h:panelGroup>

                                        <h:panelGroup id="gpLblDep" rendered="#{itemFeeManager.itemFee.feeType eq 'OtherInstitution' or itemFeeManager.itemFee.feeType eq 'OwnInstitution'  or itemFeeManager.itemFee.feeType eq 'Referral' }" >
                                            <h:outputLabel value="Department" ></h:outputLabel>
                                        </h:panelGroup>
                                        <h:panelGroup id="gpSomDepartment" rendered="#{itemFeeManager.itemFee.feeType eq 'OtherInstitution' or itemFeeManager.itemFee.feeType eq 'OwnInstitution'  or itemFeeManager.itemFee.feeType eq 'Referral' }" >
                                            <p:selectOneMenu id="somDepartment" value="#{itemFeeManager.itemFee.department}" >
                                                <f:selectItems value="#{itemFeeManager.departments}" var="dept" itemLabel="#{dept.name}" itemValue="#{dept}" ></f:selectItems>
                                            </p:selectOneMenu>
                                        </h:panelGroup>

                                        <h:panelGroup id="gpLblSpe" rendered="#{itemFeeManager.itemFee.feeType eq 'Staff'}" >
                                            <h:outputLabel value="Speciality" ></h:outputLabel>
                                        </h:panelGroup>

                                        <h:panelGroup id="gpAcSpe" rendered="#{itemFeeManager.itemFee.feeType eq 'Staff' }" >
                                            <p:autoComplete value="#{itemFeeManager.itemFee.speciality}" 
                                                            completeMethod="#{specialityController.completeSpeciality}"
                                                            var="spe" 
                                                            itemLabel="#{spe.name}" 
                                                            itemValue="#{spe}" 
                                                            converter="specilityCon" >
                                                <p:ajax event="itemSelect" process="@this" update="somEmployee" listener="#{itemFeeManager.fillStaff()}" ></p:ajax>
                                            </p:autoComplete>
                                        </h:panelGroup>

                                        <h:panelGroup id="gpLblEmp" rendered="#{itemFeeManager.itemFee.feeType eq 'Staff'}" >
                                            <h:outputLabel value="Doctor/Staff" ></h:outputLabel>
                                        </h:panelGroup>
                                        <h:panelGroup id="gpSomEmp" rendered="#{itemFeeManager.itemFee.feeType eq 'Staff'}" >
                                            <p:selectOneMenu id="somEmployee" value="#{itemFeeManager.itemFee.staff}" >
                                                <f:selectItems value="#{itemFeeManager.staffs}" var="staff" itemLabel="#{staff.person.nameWithTitle}" itemValue="#{staff}" ></f:selectItems>
                                            </p:selectOneMenu>
                                        </h:panelGroup>

                                        <h:outputLabel value="Fee" ></h:outputLabel>
                                        <h:inputText value="#{itemFeeManager.itemFee.fee}" ></h:inputText>

                                        <h:outputLabel value="Foreigner Fee" ></h:outputLabel>
                                        <h:inputText value="#{itemFeeManager.itemFee.ffee}" ></h:inputText>




                                    </p:panelGrid>

                                </p:panel>

                            </p:dialog>

                            <p:panel header="Current Fees" id="gpFees" >

                                <p:commandButton value="Update" action="#{itemFeeManager.updateFee}" 
                                                 process="@this tblFees" 
                                                 update=":#{p:component('lblTotal')} :#{p:component('lblTotalF')}" ></p:commandButton>

                                <p:dataTable id="tblFees" value="#{itemFeeManager.itemFees}" var="f" rowIndexVar="n" rowKey="#{f.id}" >
                                    <p:column headerText="No" width="30" >
                                        <p:outputLabel value="#{n+1}" ></p:outputLabel>
                                    </p:column>
                                    <p:column headerText="Name" >
                                        <p:inputText id="txtFeeName" value="#{f.name}" >
                                            <p:ajax process="txtFeeName" event="blur" listener="#{itemFeeManager.updateFee(f)}" update=":#{p:component('lblTotal')}" ></p:ajax>
                                        </p:inputText>
                                    </p:column>
                                    <p:column headerText="Type" >
                                        <h:outputLabel id="txtFeeType" value="#{f.feeType.label}" >
                                        </h:outputLabel>
                                    </p:column>
                                    <p:column headerText="Discount Allowed" >
                                        <p:selectBooleanButton value="#{f.discountAllowed}" offLabel="Discounts NOT Allowed" onLabel="Discounts Allowed" >
                                            <p:ajax process="@this" listener="#{itemFeeManager.updateFee(f)}" ></p:ajax>
                                        </p:selectBooleanButton>
                                    </p:column>



                                    <p:column headerText="For" >

                                        <h:panelGroup rendered="#{f.institution ne null}" >
                                            <h:outputLabel value="#{f.institution.name}" >
                                            </h:outputLabel>
                                            <p:spacer height="1" width="2" ></p:spacer>

                                        </h:panelGroup>
                                        <h:panelGroup rendered="#{f.speciality ne null}" >
                                            <h:outputLabel value="#{f.speciality.name}" >
                                            </h:outputLabel>
                                            <p:spacer height="1" width="2" ></p:spacer>
                                        </h:panelGroup>

                                        <h:panelGroup rendered="#{f.department ne null}" >
                                            <p:outputLabel value=" - " ></p:outputLabel>
                                            <h:outputLabel value="#{f.department.name}" >
                                            </h:outputLabel>
                                        </h:panelGroup>

                                        <h:panelGroup rendered="#{f.staff ne null}" >
                                            <p:outputLabel value=" - " ></p:outputLabel>
                                            <h:outputLabel value="#{f.staff.person.nameWithTitle}" >
                                            </h:outputLabel>
                                        </h:panelGroup>
                                    </p:column>
                                    <p:column headerText="Value" >
                                        <p:inputText id="txtFeeVal" value="#{f.fee}" >
                                            <p:ajax process="txtFeeVal" event="blur" listener="#{itemFeeManager.updateFee(f)}" update=":#{p:component('lblTotal')}" ></p:ajax>
                                        </p:inputText>
                                    </p:column>
                                    <p:column headerText="Value for Foreigners" >
                                        <p:inputText id="txtFfeeVal" value="#{f.ffee}" >
                                            <p:ajax process="txtFfeeVal" event="blur" listener="#{itemFeeManager.updateFee(f)}"  update=":#{p:component('lblTotalF')}"></p:ajax>
                                        </p:inputText>
                                    </p:column>



                                </p:dataTable>






                            </p:panel>




                        </p:panelGrid>


                        <p:commandButton rendered="false" value="Issue to Referral" ajax="false" action="#{itemFeeManager.fixIssueToReferralFees()}" ></p:commandButton>

                    </p:panel>



                </h:form>
            </ui:define>


        </ui:composition>

    </h:body>
</html>
